<template>
  <el-row class="login-container">
    <el-col :lg="16" :md="12" class="left">
      <div>
        <div>欢迎登陆</div>
        <div>Kubemanage云原生运维平台</div>
      </div>
    </el-col>
    <el-col :lg="8" class="right">
      <h2 class="title">欢迎回来</h2>
      <div>
        <span class="line"></span>
        <span>账号密码登陆</span>
        <span class="line"></span>
      </div>
      <el-form :rules="formRules" ref="formRef" :model="form" class="w-[250px]">
        <el-form-item prop="username">
          <el-input v-model.trim="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon class="el-input__icon">
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            show-password
            v-model.trim="form.password"
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon class="el-input__icon">
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="w-[250px]"
            round
            color="#626aef"
            type="primary"
            @click="onSubmit"
            :loading="loading"
            >提交
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { reactive, ref, onMounted, onBeforeUnmount } from "vue";
// 路由
import { useRouter } from "vue-router";
// pinia
import { useUserCenter } from "~/pinia/useUserCenter.js";

// 通知
import { message } from "~/utils/utils.js";

const router = useRouter();
const UserCenter = useUserCenter();

// do not use same name with ref
const form = reactive({
  username: "",
  password: "",
});

const formRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const formRef = ref(null);

const loading = ref(false);

const onSubmit = () => {
  formRef.value.validate((vaild) => {
    if (vaild) {
      loading.value = true;
      UserCenter.login(form.username, form.password)
        .then((res) => {
          if (res) {
            message("登陆成功");
            router.push("/");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
};

// 监听回车时间的方法
function onKeyUp(e) {
  if (e.key === "Enter") onSubmit();
}

onMounted(() => {
  // 添加键盘的监听
  document.addEventListener("keyup", onKeyUp);
});
onBeforeUnmount(() => {
  document.removeEventListener("keyup", onKeyUp);
});
</script>

<style>
.login-container {
  @apply min-h-screen bg-indigo-500;
}

.login-container .left {
  @apply flex items-center justify-center;
}

.login-container .right {
  @apply flex items-center justify-center bg-light-50 flex-col;
}

.left > div > div:first-child {
  @apply font-bold text-5xl text-light-50 mb-4;
}

.left > div > div:last-child {
  @apply text-gray-200 text-sm;
}

.right .title {
  @apply font-bold text-3xl text-gray-800;
}

.right > div {
  @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
}

.right .line {
  @apply h-[1px] w-16 bg-gray-200;
}
</style>
